<?php
/**
 * Created by PhpStorm.
 * User: xiewenfeng
 * Date: 2020/11/29
 * Time: 23:16
 */

namespace app\common\model;


use think\Model;

class WxAppMsg extends Model
{
    protected $name = "wxapp_msg";

    public function addMsg($relate_id, $data, $openid, $template_id, $page)
    {
        $this->save([
            'data' => json_encode($data),
            'open_id' => $openid,
            'template_id' => $template_id,
            'page' => $page,
            'create_time' => time(),
            'status' => 0,
            'relate_id' => $relate_id
        ]);
    }

    public function msgSended()
    {
        $this->status = 1;
        $this->update_time = time();
        $this->save();
    }

    public function getUnsendMsgs()
    {
        return self::all(function ($query){
            $query->where('status', 0)->limit(100)->order('id', 'asc');
        });
    }
}
